/**
 * LayUI layer loading控件扩展
 */
layui.define(['layer', 'yjslayer'], function (exports) {
    "use strict";

    var $ = layui.jquery,
        layer = layui.layer,
        yjslayer = layui.yjslayer,
        MOD_NAME = 'yjsloading';

    // 获取csrf-token
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    var LoadingLayer = {
        // 加载验证码
        loadingCaptcha: function (url) {
            var loading = layer.load(0, {
                shade: [0.5, '#FFF']
            }, {time: 10 * 1000});

            $.ajax({
                url: url,
                type: 'post',
            })
                .done(function (data) {
                    $(".getLVcode").attr("src", data);
                })
                .fail(function () {
                    yjslayer.failMsg('验证码获取失败，请刷新重试');
                })
                .always(function () {
                    layer.close(loading);
                });
        },
        // ajax确认对话框
        ajaxConfirm: function (title, msg, url, params,callback) {
            // var qrUrl = '/vcode/path/form';
            var layerConfirm = layer.confirm(msg, {
                title: title,
                resize: false,
                btn: ['确定', '取消'],
                btnAlign: 'c',
                anim: 1,
                icon: 3
            }, function () {
                var loading = layer.load(0, {
                    shade: [0.5, '#FFF']
                }, {time: 10 * 1000});

                $.ajax({
                    url: url,
                    type: 'post',
                    dataType: 'json',
                    data: params,
                })
                    .done(function (data) {
                        if (data['rspStatus'] === true) {
                            yjslayer.successMsg(data['rspMsg']);
                            location.reload();

                            // 点击确认关闭当前编辑layer
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);

                        } else {
                            // LoadingLayer.loadingCaptcha(qrUrl);
                            yjslayer.failMsg(data['rspMsg']);
                        }
                    })
                    .fail(function (data) {
                        // LoadingLayer.loadingCaptcha(qrUrl);
                        yjslayer.failMsg('功能执行失败，请稍后重试');
                    })
                    .always(function (data) {
                        callback(data);
                        // 关闭loading
                        layer.close(loading);
                        // 关闭confirm
                        layer.close(layerConfirm);
                    });
            }, function () {

            });
        },
        // ajax后端自动提交
        ajaxAutoSubmit: function (url, params,callback) {
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: params,
            })
                .done(function (data) {
                    if (data['rspStatus'] === true) {
                        yjslayer.successMsg(data['rspMsg']);
                    } else {
                        yjslayer.failMsg(data['rspMsg']);
                    }
                })
                .fail(function (data) {
                    yjslayer.failMsg('功能执行失败，请稍后重试');
                })
                .always(function (data) {
                    callback(data);
                });
        },
        // 退出系统
        exitSystem: function (title, msg, url) {
            layer.confirm(msg, {
                title: title,
                resize: false,
                btn: ['确定', '取消'],
                btnAlign: 'c',
                anim: 1,
                icon: 3
            }, function () {
                location.href = url;
            }, function () {
                yjslayer.successMsg('欢迎您回来！');
            });
        },
        // 取消编辑确认对话框
        cancelConfirm: function (title, msg) {
            layer.confirm(msg, {
                title: title,
                resize: false,
                btn: ['确定', '取消'],
                btnAlign: 'c',
                anim: 1,
                icon: 3
            }, function () {
                location.reload();
            }, function () {
                yjslayer.successMsg('请继续您正在执行的操作！');
            });
        },
        // 弹出层取消编辑确认对话框
        popupCancelConfirm: function (title, msg, popupIndex) {
            layer.open({
                type: 0
                , title: title
                , content: msg
                , btn: ['确认', '取消']
                , btnAlign: 'c'
                , anim: 1
                , icon: 3
                , zIndex: layer.zIndex // 当前窗体层叠顺序(layui自动维护)
                , success: function (layero, index) { // 弹出成功回调
                    // 置顶当前窗体
                    layer.setTop(layero);
                }
                , yes: function (index, layero) { // 确定按钮回调
                    layer.close(index);
                    layer.close(popupIndex);
                }
                , btn2: function (index, layero) { // 取消按钮回调
                    layer.close(index);
                }
                , cancel: function (index, layero) { // 关闭窗体成功回调
                    layer.close(index);
                }
            });
        },
        // ajax确认对话框
        exportConfirm: function (title, msg, url) {
            var layerConfirm = layer.confirm(msg, {
                title: title,
                resize: false,
                btn: ['确定', '取消'],
                btnAlign: 'c',
                anim: 1,
                icon: 3
            }, function () {
                window.location.href = url;
                // 关闭confirm
                layer.close(layerConfirm);
            }, function () {

            });
        },
        // ajax确认对话框
        ajaxImportConfirm: function (title, msg, url, params,callback) {
            var qrUrl = '/vcode/path/form';
            var layerConfirm = layer.confirm(msg, {
                title: title,
                resize: false,
                btn: ['确定', '取消'],
                btnAlign: 'c',
                anim: 1,
                icon: 3
            }, function () {
                var loading = layer.load(0, {
                    shade: [0.5, '#FFF']
                }, {time: 10 * 1000});

                $.ajax({
                    url: url,
                    type: 'post',
                    dataType: 'json',
                    data: params,
                })
                    .done(function (data) {
                        if (data['rspStatus'] === true) {
                            yjslayer.successMsg('数据导入成功');
                        } else {
                            LoadingLayer.loadingCaptcha(qrUrl);
                            yjslayer.failMsg(data['rspMsg']);
                        }
                    })
                    .fail(function (data) {
                        LoadingLayer.loadingCaptcha(qrUrl);
                        yjslayer.failMsg('功能执行失败，请稍后重试');
                    })
                    .always(function (data) {
                        callback(data);
                        // 关闭loading
                        layer.close(loading);
                        // 关闭confirm
                        layer.close(layerConfirm);
                    });
            }, function () {

            });
        },
    };

    /**
     * 导出插件
     */
    exports(MOD_NAME, LoadingLayer);
});